<template>
  <div class="login container">
    <Header></Header>
    <section>
      <div class="login-tel">
        <input
          type="text"
          placeholder="请输入手机号"
          pattern="[0-9*]"
          v-model.trim="userTel" />
      </div>
      <div class="login-code">
        <input
          type="text"
          placeholder="请输入短信验证码"
          pattern="[0-9*]"
          v-model.trim="userCode" />
        <button @click="sendCode" :disabled="disabled">{{ codeMsg }}</button>
      </div>
      <div class="login-btn" @click="login">登 录</div>
      <div class="tab">
        <span @click="goUserLogin">密码登录</span>
        <span @click="goRegister">快速注册</span>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue'
import Header from '@/components/common/Header.vue'
import { Toast } from 'mint-ui'
//引入封装后的axiox
import http from '@/common/api/request'

import { mapMutations } from 'vuex'
export default {
  components: { Tabbar, Header },
  name: 'Login',
  data() {
    return {
      disabled: false,
      userTel: '',
      userCode: '',
      rules: {
        //手机号验证
        userTel: {
          rule: /^1[23456789]\d{9}$/,
          msg: '手机号不能为空,且为11位数字',
        },
      },
      codeTime: 6,
      codeMsg: '获取短信验证码',
      code: '',
    }
  },
  methods: {
    ...mapMutations(['USER_LOGIN']),
    //跳转到密码登录
    goUserLogin() {
      this.$router.push('/userLogin')
    },
    //跳转到注册
    goRegister() {
      this.$router.push('/register')
    },
    //发送手机验证码
    sendCode() {
      //验证
      if (!this.validata('userTel')) return
      http
        .$axios({
          url: '/api/code',
          method: 'Post',
          data: {
            phone: this.userTel,
          },
        })
        .then((res) => {
          if (res.success) {
            this.code = res.data
            console.log(this.code)
          }
        })

      //禁用按钮
      this.disabled = true
      //倒计时
      let timer = setInterval(() => {
        --this.codeTime
        this.codeMsg = `${this.codeTime}S后可重新发送`
      }, 1000)
      //判断什么时候停止定时器
      setTimeout(() => {
        clearInterval(timer)
        this.codeTime = 6
        this.disabled = false
        this.codeMsg = '获取短信验证码'
      }, 6000)
    },
    //验证信息提示
    validata(key) {
      let bool = true
      if (!this.rules[key].rule.test(this[key])) {
        Toast(this.rules[key].msg)
        bool = false
      }
      return bool
    },
    login() {
      //验证
      if (!this.validata('userTel')) return
      if (this.code == this.userCode) {
        //验证码正确，发送请求保存数据
        http
          .$axios({
            url: '/api/addUser',
            method: 'Post',
            data: {
              phone: this.userTel,
            },
          })
          .then((res) => {
            if (!res.success) return
            //登录成功，跳转页面,储存用户信息
            this.USER_LOGIN(res.data)
            this.$router.push('/my')
          })
      } else {
        Toast('验证码不正确！')
      }
    },
  },
}
</script>

<style scoped lang="scss">
section {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.32rem;
  background-color: #f5f5f5;
  div {
    margin: 0.266666rem 0;
    width: 8.933333rem;
    height: 1rem;
  }
  input {
    box-sizing: border-box;
    padding: 0 0.3rem;
    line-height: 1rem;
    background-color: #fff;
    border-radius: 8px;
  }
  .login-tel {
    margin-top: 0.8rem;
    input {
      width: 8.933333rem;
    }
  }
  .login-code {
    display: flex;
    justify-content: space-between;
    input {
      flex: 1;
    }
    button {
      margin-left: 15px;
      padding: 0 0.533333rem;
      line-height: 1rem;
      color: #fff;
      background-color: #b0352f;
      border: 0;
      border-radius: 4px;
    }
  }
  .login-btn {
    line-height: 1rem;
    background-color: #b0352f;
    border-radius: 6px;
    font-size: 0.48rem;
    text-align: center;
    color: #fff;
  }
  .tab {
    display: flex;
    justify-content: space-between;
    font-size: 0.373333rem;
  }
}
</style>
